package com.scienview.core.interceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.scienview.core.context.ContextHelper;
import com.scienview.core.context.ContextHolder;

public class ContextInterceptor extends HandlerInterceptorAdapter{

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object object) throws Exception {
		request.setAttribute("webRoot", request.getContextPath());
		HandlerMethod handler=(HandlerMethod)object;
		ContextHelper.parse(request, response, handler);
		return true;
	}
	
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		if(modelAndView!=null){
			RequestMapping classMapping=ContextHolder.getContext().getHandler().getBeanType().getAnnotation(RequestMapping.class);
			RequestMapping mapping=ContextHolder.getContext().getHandler().getMethodAnnotation(RequestMapping.class);
			
			modelAndView.setViewName(classMapping.value()[0]+"/"+mapping.value()[0]);
		}
		System.out.println("经过了Controller 进行美化");
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
	throws Exception {
		System.out.println("经过了Controller 出来啦");
		ContextHelper.clear();
	}
	
	

}
